/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hyper.market.beans;

import database.DBAccessController;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

/**
 *
 * @author Mikeno
 */
public class NotificationBean {

    private String desc;
    private int notificationid;
    private String createdDate;
    private int action;
    private int createdBy;
    private String storeName;
    private String staffName;
    private int stockdetailid;

    public NotificationBean() {
    }

    private NotificationBean(int notificationid, String desc, String date, int action, String staffname, String storename, int stockdetailid) {
        setNotificationid(notificationid);
        setDesc(desc);
        setCreatedDate(date);
        setAction(action);
        setStaffName(staffname);
        setStoreName(storename);
        setStockdetailid(stockdetailid);
    }

    public ArrayList retrieveNotificationById(int storeid, String jndi) {

        DBAccessController db = new DBAccessController(jndi);
        String sql = "";
        if (storeid == 0) {
            // take all else take oni manager
            sql = "select n.notificationid, n.description, n.createdDate,n.action ,s.loginname, st.storename, n.stockdetailid from notification n, staff s, store st "
                    + "where n.storeid = st.storeid and s.staffid = n.createdBy and n.action between 1 and 3 ORDER BY st.storename ASC";
        } else {
            sql = "select n.notificationid, n.description, n.createdDate,n.action ,s.loginname, st.storename, n.stockdetailid from notification n, staff s, store st "
                    + "where n.storeid = st.storeid and s.staffid = n.createdBy and n.storeid = '" + storeid + "' and n.action between 4 and 6 ORDER BY st.storename ASC";
        }
        DateFormat df = new SimpleDateFormat("dd MMM yyyy");
        ArrayList list = new ArrayList();
        NotificationBean nb = null;
        try {
            ResultSet rs = db.execSelectQuery(sql);
            while (rs.next()) {
                nb = new NotificationBean(rs.getInt(1), rs.getString(2), df.format(rs.getDate(3)), rs.getInt(4), rs.getString(5), rs.getString(6), rs.getInt(7));
                list.add(nb);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            db.terminate();
            return null;
        }
        db.terminate();
        return list;
    }

    public final boolean updateNotificationAction(String jndi, int nid) {
        DBAccessController db = new DBAccessController(jndi);
        String sql = "update Notification set action='0' where notificationid = '" + nid + "'";
        try {
            db.execUpdateQuery(sql);
        } catch (Exception e) {
            db.terminate();
            return false;
        }
        db.terminate();
        return true;
    }

    public final boolean rejectManagerRequest(String jndi, String storename, int stockid, int staffid, String createBy) {
        DBAccessController db = new DBAccessController(jndi);

        String sql = "select s.storeid, sd.stockName from store s, stockdetails sd , storestocks st where sd.stockdetailid = st.stockdetailid and st.storeid = s.storeid and s.storename ='" + storename + "' and sd.stockDetailid = '" + stockid + "'";
        String sql2 = "";
        String desc = "";
        try {
            ResultSet rs = db.execSelectQuery(sql);
            if (rs.next()) {
                desc = "Sorry, The Requested Discount for Item " + rs.getString("stockname") + " has been rejected";
                sql2 = "insert into Notification (createdBy,Description,createdDate,action,storeid,stockdetailid)values ('" + staffid + "','" + desc + "',curdate(),'4','" + rs.getInt("storeid") + "','" + stockid + "')";

            }
        } catch (SQLException e) {
            e.printStackTrace();
            db.terminate();
            return false;
        }
        db.terminate();
        db = new DBAccessController(jndi);
        db.execUpdateQuery(sql);
        db.terminate();
        return true;
    }

    public final boolean acceptManagerRequest(String jndi, int storeid, int stockid, int staffid, double sellingprice, String itemName) {
        DBAccessController db = new DBAccessController(jndi);

        String desc = "The requested Item : " + itemName + " Seling price has been changed to : $ " + sellingprice;
        // individual stores has a different set of actions
        // 4 - view
        String sql = "insert into Notification (createdBy,Description,createdDate,action,storeid,stockdetailid)values ('" + staffid + "','" + desc + "',curdate(),'4','" + storeid + "','" + stockid + "')";

        try {
            db.execUpdateQuery(sql);
        } catch (Exception e) {
            e.printStackTrace();
            db.terminate();
            return false;
        }
        db.terminate();
        return true;
    }

    public final boolean createRequest(String jndi, int storeid, int stockid, int staffid, String desc){

        DBAccessController db = new DBAccessController(jndi);

        String sql = "insert into Notification (createdBy,Description,createdDate,action,storeid,stockdetailid)values ('" + staffid + "','" + desc + "',curdate(),'2','" + storeid + "','" + stockid + "')";
        try {
            db.execUpdateQuery(sql);
        } catch (Exception e) {
            e.printStackTrace();
            db.terminate();
            return false;
        }
        db.terminate();
        return true;
    }

    /**
     * @return the desc
     */
    public String getDesc() {
        return desc;
    }

    /**
     * @param desc the desc to set
     */
    public final void setDesc(String desc) {
        this.desc = desc;
    }

    /**
     * @return the notificationid
     */
    public int getNotificationid() {
        return notificationid;
    }

    /**
     * @param notificationid the notificationid to set
     */
    public final void setNotificationid(int notificationid) {
        this.notificationid = notificationid;
    }

    /**
     * @return the createdDate
     */
    public String getCreatedDate() {
        return createdDate;
    }

    /**
     * @param createdDate the createdDate to set
     */
    public final void setCreatedDate(String createdDate) {
        this.createdDate = createdDate;
    }

    /**
     * @return the action
     */
    public int getAction() {
        return action;
    }

    /**
     * @param action the action to set
     */
    public final void setAction(int action) {
        this.action = action;
    }

    /**
     * @return the createdBy
     */
    public int getCreatedBy() {
        return createdBy;
    }

    /**
     * @param createdBy the createdBy to set
     */
    public void setCreatedBy(int createdBy) {
        this.createdBy = createdBy;
    }

    /**
     * @return the storeName
     */
    public String getStoreName() {
        return storeName;
    }

    /**
     * @param storeName the storeName to set
     */
    public final void setStoreName(String storeName) {
        this.storeName = storeName;
    }

    /**
     * @return the staffName
     */
    public String getStaffName() {
        return staffName;
    }

    /**
     * @param staffName the staffName to set
     */
    public final void setStaffName(String staffName) {
        this.staffName = staffName;
    }

    /**
     * @return the stockdetailsid
     */
    public int getStockdetailid() {
        return stockdetailid;
    }

    /**
     * @param stockdetailsid the stockdetailsid to set
     */
    public final void setStockdetailid(int stockdetailid) {
        this.stockdetailid = stockdetailid;
    }
}
